package com.dotools.toutiaolibrary.util;

import android.view.View;
import android.view.ViewGroup;

import com.bykv.vk.openvk.ISplashClickEyeListener;
import com.bykv.vk.openvk.TTSphObject;

import java.lang.ref.SoftReference;

public class SplashClickEyeUtils {


    //显示点睛开屏
    public static void initSplashClickEyeData(ViewGroup decorView, ViewGroup splashViewContainer) {
        SplashClickEyeManager splashClickEyeManager = SplashClickEyeManager.getInstance();
        boolean isSupportSplashClickEye = splashClickEyeManager.isSupportSplashClickEye();
        if (!isSupportSplashClickEye) {
            splashClickEyeManager.clearSplashStaticData();
            return;
        }
        View splashClickEyeView = addSplashClickEyeView(decorView, splashViewContainer);
        if (splashClickEyeView != null) {
            TTSphObject splashAd = splashClickEyeManager.getSplashAd();
            if (splashAd != null) {
                SplashClickEyeListener splashClickEyeListener = new SplashClickEyeListener(splashClickEyeView, splashAd);
                splashAd.setSplashClickEyeListener(splashClickEyeListener);
            }
        }
    }

    private static View addSplashClickEyeView(ViewGroup decorView, ViewGroup splashViewContainer) {
        SplashClickEyeManager splashClickEyeManager = SplashClickEyeManager.getInstance();
        TTSphObject splashAd = splashClickEyeManager.getSplashAd();
        return splashClickEyeManager.startSplashClickEyeAnimationInTwoActivity(decorView, splashViewContainer, new SplashClickEyeManager.AnimationCallBack() {
            @Override
            public void animationStart(int animationTime) {
            }

            @Override
            public void animationEnd() {
                splashAd.splashClickEyeAnimationFinish();
            }
        });
    }


    static class SplashClickEyeListener implements ISplashClickEyeListener {
        private SoftReference<View> mSplashView;
        private SoftReference<TTSphObject> mSplashAd;

        public SplashClickEyeListener(View splashView, TTSphObject splashAd) {
            mSplashView = new SoftReference(splashView);
            mSplashAd = new SoftReference<TTSphObject>(splashAd);
        }


        @Override
        public void onSplashClickEyeAnimationStart() {

        }

        @Override
        public void onSplashClickEyeAnimationFinish() {
            //接收点击关闭按钮的事件将开屏点睛移除。
            if (mSplashView != null && mSplashView.get() != null) {
                mSplashView.get().setVisibility(View.GONE);
                UIUtils.removeFromParent(mSplashView.get());
                mSplashView = null;
                mSplashAd = null;
            }
            SplashClickEyeManager.getInstance().clearSplashStaticData();
        }

        @Override
        public boolean isSupportSplashClickEye(boolean b) {
            return false;
        }
    }
    //点睛开屏end
}
